//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

namespace Microsoft.Research.ScientificWorkflow.ManagementStudioModels
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using SR = Microsoft.Research.DataLayer;
    using Microsoft.Research.ScientificWorkflow.ManagementStudioModels;
    using System.Collections.ObjectModel;
    using System.Windows.Media.Imaging;
    using Microsoft.Research.ScientificWorkflow.TridentUtilities;

    /// <summary>
    /// AssemblyPackageViewModel
    /// </summary>
    public class AssemblyPackageViewModel : BaseRegMgrModel
    {
        /// <summary>
        /// Constructor
        /// </summary>
        public AssemblyPackageViewModel(string name)
        {
            this.Name = name;
            this.Label = this.Name;
            this.Id = Guid.NewGuid();
            this.IsValidAssemblyModel = false;
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="assemblyPackage"></param>
        public AssemblyPackageViewModel(SR.AssemblyPackage assemblyPackage)
        {
            this.Name = assemblyPackage.Name;
            this.Label = string.IsNullOrEmpty(assemblyPackage.Label) ? assemblyPackage.Name : assemblyPackage.Label;
            this.PackageReferences = new ObservableCollection<AssemblyPackageAssociationViewModel>();
            this.Id = (assemblyPackage as SR.IObject).ID;
            this.IsValidAssemblyModel = true;
        }

        /// <summary>
        /// get and set PackageReference Collection
        /// </summary>
        public ObservableCollection<AssemblyPackageAssociationViewModel> PackageReferences { get; set; }

        /// <summary>
        /// get and set IsValidAssemblyModel
        /// </summary>
        public bool IsValidAssemblyModel { get; set; }

        /// <summary>
        ///  get and set Label for the Reference List
        /// </summary>
        public string ListLabel { get; set; }

        /// <summary>
        /// Set Activity Icon
        /// </summary>
        public void AssignActivityIcon()
        {
            this.ItemIcon = CreateImage("pack://Application:,,,/Microsoft.Research.ScientificWorkflow.TridentUtilities;component/Images/ManagementStudio/assemblydllicon.png");
        }

        /// <summary>
        /// Overridden tostring method. For automation Ids.
        /// </summary>
        /// <returns>The string representation of the object.</returns>
        public override string ToString()
        {
            if (!string.IsNullOrEmpty(this.Name))
            {
                return this.Name;
            }
            return base.ToString();
        }

        /// <summary>
        /// Creates the image.
        /// </summary>
        /// <param name="imagePath">The image path.</param>
        /// <returns></returns>
        private BitmapImage CreateImage(string imagePath)
        {
            BitmapImage image = null;
            try
            {
                image = new BitmapImage(new Uri(imagePath, UriKind.Absolute));
            }
            catch (Exception ex)
            {
                TridentErrorHandler.HandleUIException(ex);
            }

            return image;
        }

    }
}
